package framework;

import android.util.Log;

import com.tetris.project.Shr_Res;

import framework.interfaces.Updatable;

@SuppressWarnings("unused")
public class Physics {

	public static double gravity = -9.8;
	static double mVelocity;
	static double mY;
	static Updatable updatable;
	static double pausedTime, resumedTime, resumedTime1, resumedTime2;
	public static double gameTime, mInitialTime;
	static final double SECOND = 1000.0;
	static double currentTime;
	private static double sendDownHeight = 0;
	
	public static void init(double initialVelocity, double initialHeight, Updatable upd)
	{
		mVelocity = initialVelocity;
		mY = initialHeight;
		updatable = upd;
		mInitialTime = 0;
		pausedTime = 0;
		resumedTime = 0;
		resumedTime1 = 0;
		resumedTime2 = 0;
		gameTime = 0;
		gravity = getGravity();
	}

	public static void updatePhysics(double time)
	{
		//Log.d(Shr_Res.TAG, "sendDownPiece"+Shr_Res.sendDownPiece);
		currentTime = 0;
		
		double calculatedHeight = 0;
		double bla = 0;
		
		//currentTimeSince1970;

		gravity = getGravity();

		if(time < mInitialTime)
			return;

		if(mInitialTime > 0) {
			currentTime = time - mInitialTime;
			currentTime = currentTime / SECOND;
		}
		else
			mInitialTime = time;
			
		//Log.d("Physics", "Current Time = "+currentTime);
		bla = time - mInitialTime;
		//Log.d("Physics", "Time - mInitialTime"+bla);
		//resumedTime = getResumedTime();

		gameTime = currentTime-resumedTime;
		//Log.d("Physics", "Game Time = "+(gameTime));

		if (!Shr_Res.pauseGame){//if the game was paused don't calculate height anymore
			if(Shr_Res.sendDownPiece){
				//if (time - mInitialTime >= 10){
					mInitialTime = time;
					sendDownHeight = sendDownHeight+21;
				//calculatedHeight = (100 * gameTime - (gravity * Math.pow(gameTime,2) /2));
					updatable.update(Math.abs(sendDownHeight));
				//}
			}
			else{
				gameTime = currentTime-resumedTime;
				calculatedHeight = ((getVelocity() * gameTime) - (gravity * Math.pow(gameTime,2) /2));
				sendDownHeight = calculatedHeight;
				updatable.update(Math.abs(calculatedHeight));
			}
			
		}
		//Log.d("Physics", "CalculatedHeight = "+calculatedHeight);
		//Log.d("Physics", "Time: " + currentTime + "height : " + calculatedHeight);
		//engine.Board.printTableStatus()
	}

	public static void resetPhysics()
	{
		mInitialTime = 0;
	}
	
	public static void setVelocity(double vel)
	{
		mVelocity = vel;
	}

	public static void setPausedTime(double time){
		pausedTime = time;
//		Log.d("Physics", "Paused Time = "+pausedTime);
	}
	
	public static void setResumedTime(){
		resumedTime = currentTime - pausedTime;
//		Log.d("Physics", "Resumed Time = "+resumedTime);
	}
	
	public static void resetResumedTime(){
		resumedTime = 0;
	}

	public static double getResumedTime(){
		return (double) resumedTime2-resumedTime1;
	}
	
	public static double getGravity(){
		switch (Shr_Res.level){
		case 0:
			gravity = -1.6;
			break;
		case 1:
			gravity = -3.6;
			break;
		case 2:
			gravity = -9.8;
			break;
		case 3:
			gravity = -10.4;
			break;
		case 4:
			gravity = -20.87;
			break;
		case 5:
			gravity = -274;
			break;
		}
		//Log.d(Shr_Res.TAG, "Gravity set to ="+gravity);
		return gravity;
	}
	
	public static double getVelocity() {
		switch(Shr_Res.dificulty) {
		case 0:
			setVelocity(0.0);
			break;
		case 1:
			setVelocity(20.0);
			break;
		case 2:
			setVelocity(40.0);
			break;
		case 3:
			setVelocity(60.0);
			break;
		}
		return mVelocity;
}
}
